package com.xoa.service.party;

import com.xoa.dao.party.IntegralManagerMapper;
import com.xoa.dao.party.OrgDepartmentMapper;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.party.IntegralManager;
import com.xoa.model.party.IntegralManagerModel;
import com.xoa.model.party.OrgDepartment;
import com.xoa.model.party.OrgDepartmentWithBLOBs;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by 程叶同 on 2018/1/12.
 */
@Service
public class IntegralManagerService {
    @Autowired
    IntegralManagerMapper integralManagerMapper;

    @Autowired
    private OrgDepartmentMapper orgDepartmentMapper;

    /**
    * @Author 程叶同
    * @Description 添加
    * @Date 12:49 2018/1/12
    */
    public BaseWrapper addData(HttpServletRequest request, IntegralManager integralManager){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(integralManager!=null){
            integralManager.setStatus("0"); //默认状态为0 未审核 1为通过 2为未通过
            integralManager.setStepStatus("1"); //1 为党支部审批 2为考核小组审批
            integralManager.setCreateTime(new Date());
            integralManager.setAssessmentTeamaudit("待审核");
            integralManager.setPartyBranchaudit("待审核");
            int num = integralManagerMapper.insert(integralManager);
            if(num>0){
                baseWrapper.setMsg("添加成功");
                baseWrapper.setFlag(true);
                baseWrapper.setStatus(true);
            }
        }
        return  baseWrapper;
    }

    public BaseWrapper deleteData(Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(id!=null&&id!=0){
            int i = integralManagerMapper.deleteByPrimaryKey(id);
            if(i>0){
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("ok");
            }
        }
        return  baseWrapper;
    }
    /**
    * @Author 程叶同
    * @Description 办理
    * @Date 13:28 2018/1/12
    */

    public BaseWrapper updateData(IntegralManager integralManager){
        BaseWrapper baseWrapper=new BaseWrapper();
        Integer imid = integralManager.getImid();
        Integer stepStatus=0;
        Integer status=0;
        if(imid!=null&&imid!=0){
            IntegralManager integralManager1 = integralManagerMapper.selectByPrimaryKey(imid);
            stepStatus=Integer.parseInt(integralManager1.getStepStatus()); //1 为党支部审批 2为考核小组审批
            integralManager.setStepStatus(integralManager1.getStepStatus());
            integralManager.setCreateTime(integralManager1.getCreateTime());
            if(integralManager.getScore()==null||integralManager.getScore()==""){
                integralManager.setScore(integralManager1.getScore());
            }
            if(integralManager.getScore()!=null&&(integralManager1.getScore()!=null||integralManager1.getScore()!="")){
                integralManager.setScoreOld(integralManager1.getScore());
            }
            integralManager.setScoreStandard(integralManager.getScoreStandard());
            integralManager.setPartyBranchaudit(integralManager1.getPartyBranchaudit());//党支部初审
            integralManager.setAssessmentTeamaudit(integralManager1.getAssessmentTeamaudit());//考核小组审核
            if(integralManager.getStatus()!=null){
                status=Integer.parseInt(integralManager.getStatus());
            }else{
                integralManager.setStatus(integralManager1.getStatus());
            }

        }
        if(stepStatus==1&&status==1){
            integralManager.setStepStatus("2");  //移交
            integralManager.setPartyBranchaudit("通过");
            integralManager.setStatus("0");
        }
        if(stepStatus==1&&status==2){
            integralManager.setPartyBranchaudit("未通过");
        }
        if(stepStatus==2&&status==1){
            integralManager.setAssessmentTeamaudit("通过");
        }
        if(stepStatus==2&&status==2){
            integralManager.setAssessmentTeamaudit("未通过");
        }

        int i = integralManagerMapper.updateByPrimaryKey(integralManager);
        if(i>0){
            baseWrapper.setFlag(true);
            baseWrapper.setStatus(true);
            baseWrapper.setMsg("ok");
        }
        return  baseWrapper;
    }


    public BaseWrapper getDataById(HttpServletRequest request, Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        if(id!=0&&id!=null){
            IntegralManager integralManager = integralManagerMapper.selectByPrimaryKey(id);
            if(integralManager!=null){
                List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(integralManager.getAttachmentName(), integralManager.getAttachmentId(), sqlType, GetAttachmentListUtil.MODULE_parameter);
                integralManager.setAttachmentList(attachments);
                OrgDepartmentWithBLOBs orgDepartmentWithBLOBs=null;
                if(integralManager.getPartyBranch()!=null){
                    orgDepartmentWithBLOBs = orgDepartmentMapper.selectByPrimaryKey(Integer.parseInt(integralManager.getPartyBranch()));
                }
                if(orgDepartmentWithBLOBs!=null){
                    integralManager.setPartyBranchName(orgDepartmentWithBLOBs.getOrgDeptName());
                }
                baseWrapper.setMsg("ok");
                baseWrapper.setFlag(true);
                baseWrapper.setStatus(true);
                baseWrapper.setData(integralManager);
            }
        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description  查询全部
    * @Date 13:38 2018/1/12
    */

    public BaseWrapper getList(HttpServletRequest request, Integer page, Integer pageSize, boolean useFlag, String startTime, String endTime, String stepStatus, String status){
        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        BaseWrapper baseWrapper=new BaseWrapper();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map1=new HashMap<String,Object>();
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("pageParams",pageParams);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("stepStatus",stepStatus);
        map.put("status",status);
        map1.put("startTime",startTime);
        map1.put("endTime",endTime);
        map1.put("stepStatus",stepStatus);
        map1.put("status",status);
        List<IntegralManager> integralManagers = integralManagerMapper.selectList(map);
        int i = integralManagerMapper.totalItems(map1);
        if(integralManagers!=null) {
            for (IntegralManager im : integralManagers) {
                List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(im.getAttachmentName(), im.getAttachmentId(), sqlType, GetAttachmentListUtil.MODULE_parameter);
                im.setAttachmentList(attachments);
                if(im.getPartyBranch()==null){
                    continue;
                }
                OrgDepartmentWithBLOBs orgDepartmentWithBLOBs = orgDepartmentMapper.selectByPrimaryKey(Integer.parseInt(im.getPartyBranch()));
                if(orgDepartmentWithBLOBs!=null){
                    im.setPartyBranchName(orgDepartmentWithBLOBs.getOrgDeptName());
                }
            }
            baseWrapper.setData(integralManagers);
            baseWrapper.setStatus(true);
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("ok");
            baseWrapper.setTotle(pageParams.getTotal());
        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description  统计
    * @Date 13:38 2018/1/12
    */
    public BaseWrapper getDataByType(Integer page, Integer pageSize, boolean useFlag, String type, String timeType, String partyId, String paixu){
        BaseWrapper baseWrapper=new BaseWrapper();
        List<OrgDepartmentWithBLOBs> orgDepartmentWithBLOBs = orgDepartmentMapper.queryDepIdList(null);
        List<Integer> list1=new ArrayList<Integer>();
        /*if(orgDepartmentWithBLOBs!=null){
            for(OrgDepartmentWithBLOBs org:orgDepartmentWithBLOBs){
                id.add(org.getOrgDeptId());
            }
        }*/
        List<Integer> reOrg=null;
        if(partyId!=null&&!"".equals(partyId)){
            list1.add(Integer.parseInt(partyId));
            reOrg= this.reOrgId(orgDepartmentWithBLOBs, Integer.parseInt(partyId), list1);
        }

        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map1=new HashMap<String,Object>();
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("pageParams",pageParams);
        map.put("timeType",timeType);
        map1.put("timeType",timeType);
        map1.put("id",reOrg);
        map.put("id",reOrg);
        if("year".equals(type)){
            map.put("type","%Y");
            map1.put("type","%Y");
        }
        if("month".equals(type)){
            map.put("type","%Y-%m");
            map1.put("type","%Y-%m");
        }
        List<IntegralManagerModel> dataByType = integralManagerMapper.getDataByType(map);
        List<Integer> num=integralManagerMapper.getDataNum(map1);
        if(dataByType!=null){
            for(IntegralManagerModel modelyear:dataByType){
                OrgDepartmentWithBLOBs orgDepartment = orgDepartmentMapper.selectByPrimaryKeyById(Integer.parseInt(modelyear.getPartyBranch()));
                if(orgDepartment!=null){
                    modelyear.setPartyBranchName(orgDepartment.getOrgDeptName());
                }
            }
            if(paixu!=null){
                Collections.sort(dataByType, new Comparator<IntegralManagerModel>() {
                    @Override
                    public int compare(IntegralManagerModel o1, IntegralManagerModel o2) {
                        if(o1.getScoreTotal()>o2.getScoreTotal()){
                            return  -1;
                        }
                        if(o1.getScoreTotal()==o2.getScoreTotal()){
                            return  0;
                        }
                        if(o1.getScoreTotal()<o2.getScoreTotal()){
                            return  1;
                        }
                        return 0;
                    }
                });
            }
            baseWrapper.setMsg("ok");
            baseWrapper.setFlag(true);
            baseWrapper.setStatus(true);
            baseWrapper.setData(dataByType);
            if(num!=null){
                baseWrapper.setTotle(num.size());
            }else{
                baseWrapper.setTotle(0);
            }

        }
        return baseWrapper;
    }


    /**
    * @Author 程叶同
    * @Description 加载数据
    * @Date 17:27 2018/1/15
    */
    public BaseWrapper getPartybranchData(Integer deptId){
        BaseWrapper baseWrapper=new BaseWrapper();
        List<OrgDepartment> orgDeptData = orgDepartmentMapper.getOrgDeptData();
            if(orgDeptData!=null) {
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
                baseWrapper.setData(orgDeptData);
                baseWrapper.setMsg("ok");
            }
        return  baseWrapper;
    }

    public List<Integer> reOrgId(List<OrgDepartmentWithBLOBs> orgDeplist, Integer orgdepId, List<Integer> list ){
        for (OrgDepartmentWithBLOBs orgDepartmentWithBLOBs : orgDeplist){
            if (orgdepId.equals(orgDepartmentWithBLOBs.getOrgDeptParent())){
                list.add(orgDepartmentWithBLOBs.getOrgDeptId());
                reOrgId(orgDeplist, orgDepartmentWithBLOBs.getOrgDeptId(), list);
            }
        }
        return list;
    }


    /**
     * 通过遍历判断后将赋予新的集合
     *
     * @param list
     * @reutrn List<T>
     * */
    public List<IntegralManagerModel> distinctByANewList(List<IntegralManagerModel> list){
        List<IntegralManagerModel> newList = new  ArrayList<IntegralManagerModel>();
        for(IntegralManagerModel t : list){
            if(!newList.contains(t)){
                newList.add(t);
            }
        }
        return newList;
    }
}
